package cn.iocoder.yudao.module.infra.convert;

import cn.smallbun.screw.core.util.CollectionUtils;
import com.google.common.collect.Lists;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 树结构工具类
 *
 * @author wangsong
 * @date 2023-07-07 17:20
 */
public class TreeConvert<T extends BaseTree> {

    /**
     * 角色list转换为Tree
     *
     * @param list
     * @return
     */
    public List<T> convertTree(List<T> list) {
        List<T> trees = Lists.newArrayList();

        //提取所有根节点
        Iterator<T> iterator = list.iterator();
        while (iterator.hasNext()) {
            T t = iterator.next();
            if (t.getParentId() == 0) {
                trees.add(t);
                iterator.remove();
            }
        }

        //递归获取根节点下面子节点
        for (T tree : trees) {
            List<T> adminGroups = getChildren(list, tree.getId());
            tree.setChildren(adminGroups);
        }

        return trees;
    }

    private List<T> getChildren(List<T> list, long pid) {
        //子节点列表
        List<T> childList = Lists.newArrayList();
        Iterator<T> it = list.iterator();
        while (it.hasNext()) {
            T t = it.next();
            if (pid == t.getParentId()) {
                childList.add(t);
                it.remove();
            }
        }

        //遍历 递归获取子节点的子节点
        for (T t : childList) {
            List<T> child = getChildren(list, t.getId());
            t.setChildren(child);
        }
        //递归出口  childList长度为0
        if (childList.size() == 0) {
            return Lists.newArrayList();
        }
        return childList;
    }

    public List<T> convertTree(List<T> list, Map<String, Long> countMap) {
        List<T> trees = Lists.newArrayList();

        //提取所有根节点
        Iterator<T> iterator = list.iterator();
        while (iterator.hasNext()) {
            T t = iterator.next();
            if (t.getParentId() == 0) {
                if(countMap!=null) {
                    Long value = countMap.get(String.valueOf(t.getId()));
                    if (value != null) {
                        t.setCount(Long.valueOf(value));
                    }
                }
                trees.add(t);
                iterator.remove();
            }
        }

        //递归获取根节点下面子节点
        for (T tree : trees) {
            List<T> adminGroups = getChildren(list, tree.getId(), countMap);
            tree.setChildren(adminGroups);
        }

        return trees;
    }

    private List<T> getChildren(List<T> list, long pid, Map<String, Long> countMap) {
        //子节点列表
        List<T> childList = Lists.newArrayList();
        Iterator<T> it = list.iterator();
        while (it.hasNext()) {
            T t = it.next();
            if (pid == t.getParentId()) {
                if(countMap!=null) {
                    Long value = countMap.get(String.valueOf(t.getId()));
                    if (value != null) {
                        t.setCount(Long.valueOf(value));
                    }
                }
                childList.add(t);
                it.remove();
            }
        }

        //遍历 递归获取子节点的子节点
        for (T t : childList) {
            List<T> child = getChildren(list, t.getId(), countMap);
            t.setChildren(child);
        }
        //递归出口  childList长度为0
        if (childList.size() == 0) {
            return Lists.newArrayList();
        }
        return childList;
    }

    /**
     * 根据Code递归查找
     *
     * @param list
     * @param code
     * @param result
     */
    public void findNodeByCode(List<T> list, String code, List<T> result) {
        List<T> child = list.stream().filter(t -> t.getCode().equals(code)).collect(Collectors.toList());
        //递归查找
        if (CollectionUtils.isEmpty(child)) {
            for (T t : list) {
                findNodeByCode(t.getChildren(), code, result);
            }
        } else {
            child.forEach(t -> {
                result.addAll(t.getChildren());
            });
        }
    }

}
